<?php

namespace App\Models\Student\Mo;
use App\Libs\Tools\Excel\ExcelImport;
use App\Libs\Tools\SystemCodeConstant;
use Illuminate\Support\Facades\DB;

class StudentExcelImportMO extends ExcelImport
{
    protected $titles=[
        'name',
        'old_name',
        'census_no',
        'sex',
        'nation',
        'born_date',
        'id_card',
        'hometown',
        'citizenship',
        'home_address',
        'now_address',
        'is_macao_or_taiwan',
        'is_only_child',
        'is_lodge',
        'is_learn_preschool',
        'is_leftover_child',
        ];

    public function convert_results_data(){
        $results=$this->getResults();
        $this->results=[];
        foreach ($results as $key=>$result){
            if($result['sex']=='女'){$result['sex']=SystemCodeConstant::SEX_FEMALE;}else{
                $result['sex']=SystemCodeConstant::SEX_MALE;
            }
            if(isset($result['is_macao_or_taiwan'])){
                if($result['is_macao_or_taiwan']=='是'){
                    $result['is_macao_or_taiwan']=SystemCodeConstant::YES;
                }
            }
            if(isset($result['is_only_child'])){
                if($result['is_only_child']=='是'){
                    $result['is_only_child']=SystemCodeConstant::YES;
                }
            }
            if(isset($result['is_lodge'])){
                if($result['is_lodge']=='是'){
                    $result['is_lodge']=SystemCodeConstant::YES;
                }
            }
            if(isset($result['is_learn_preschool'])){
                if($result['is_learn_preschool']=='是'){
                    $result['is_learn_preschool']=SystemCodeConstant::YES;
                }
            }
            if(isset($result['is_leftover_child'])){
                if($result['is_leftover_child']=='是'){
                    $result['is_leftover_child']=SystemCodeConstant::YES;
                }
            }
            $query=DB::table('student')->where('census_no','=',$result['census_no']);
            $cnt=$query->count();
            if($cnt>0){return $result['name']."的学籍号经在系统中存在";}
            array_push($this->results,$result);
        }
        return null;
    }
    public function verify(){
       $results=$this->getResults();
       foreach ($results as $key=>$result){
           if(!isset($result['name']) ||
              !isset($result['census_no']) ||
              !isset($result['sex']) ||
              !isset($result['nation'])||
              !isset($result['born_date'])||
              !isset($result['id_card'])||
              !isset($result['hometown'])||
              !isset($result['citizenship'])||
              !isset($result['home_address'])||
              !isset($result['now_address'])){
                return '姓名、学籍号、性别、名族、出生日期、身份证号、户籍地、国籍、家庭住址、现住地址';
           }
       }
       return null;
    }
}